//
//  UIImage+Extension.m
//  1108图片选择器
//
//  Created by Apple on 15/11/8.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "UIImage+Extension.h"

@implementation UIImage (Extension)

//等宽高比缩放
- (UIImage *)scaleImage
{
    //定义图片的宽度  新的宽度
    CGFloat newWidth = 300;
    
    if (self.size.width <= 300)
    {
        return self;
    }
    
    //计算新的图片高度
    CGFloat newHeight = newWidth * (self.size.height / self.size.width);
    
    //图片新的size
    CGSize newSize = CGSizeMake(newWidth, newHeight);
    
    //开启图形上下文
    UIGraphicsBeginImageContext(newSize);
    
    //绘图
    [self drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
    
    //获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭上下文
    UIGraphicsEndImageContext();
    
    return image;
}

@end
